import numpy as np
import keras
import matplotlib.pyplot as plt

def cal_acc(label,prediction):
    num = 0
    N = len(label)
    for i in range(len(label)):
        pred = np.argmax(prediction[i])
        if label[i] == pred:
            num += 1
    return num/N

def shuffle_data_label(data, label):
    state = np.random.get_state()
    np.random.shuffle(data)
    np.random.set_state(state)
    np.random.shuffle(label)
    return data,label

text_label = np.load("./model_file/pre_result/label_2.npy")
pred_label = np.load("./model_file/pre_result/pred_2.npy ")
text_data = np.load("./model_file/pre_result/text_2.npy")
models = keras.models.load_model("./model_file/h5_file/model_2.h5")

img_text,label = shuffle_data_label(text_data,text_label)

text_data[2].reshape((1, 256, 256, 1))

predictions = models.predict(img_text)
# print(predictions,np.argmax(predictions))
preds = []
for x in range(len(predictions)):
    pred = np.argmax(predictions[x])
    preds.append(pred)
print(np.array(preds))
acc = cal_acc(label, predictions)
print("准确率为 {:.2f}".format(acc))

# # 绘制子图
# fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12, 4))  # 创建一个1行2列的子图网格
#
# # 第一个子图：pred_label
# axs[0].bar(range(200), preds, color='blue', label='Predicted Label')
# axs[0].set_title('Predicted Labels')
# axs[0].set_xlabel('Sample Index')
# axs[0].set_ylabel('Label')
# axs[0].legend()
#
# # 第二个子图：text_label
# axs[1].bar(range(200), text_label, color='orange', label='True Label')
# axs[1].set_title('True Labels')
# axs[1].set_xlabel('Sample Index')
# axs[1].set_ylabel('Label')
# axs[1].legend()
#
# # 调整子图间距并显示图形
# plt.tight_layout()
# plt.show()